// pages/profile/share/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invite_id:0,
    invite_avatar:'',
    invite_qrcode:'',
    share_image:'',
    share_title:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShareInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.share_title,
      path: '/pages/main/index?invite_id=' + this.data.invite_id,
      imageUrl: this.data.share_image,
    }
  },
  getShareInfo(){
    const _this = this;
    app.request('profile/getShareInfo', {}, true).then((succ) => {
      _this.setData({
        invite_id: succ.data.invite_id,
        invite_avatar: succ.data.invite_avatar,
        invite_qrcode: succ.data.invite_qrcode,
        share_image: succ.data.share_image,
        share_title: succ.data.share_title
      })
    })
  },
  save: function () {
    var that = this;
    wx.showLoading({
      title: '保存中',
    })
    wx.getImageInfo({
      src: that.data.invite_qrcode,
      success: function (res) {
        that.setData({
          tempPath: res.path
        })
        that.temp();
      },
      fail(res) {
        wx.hideLoading()
      }
    })

  },
  temp: function () {
    var that = this;
    var tempPath = that.data.tempPath
    wx.saveImageToPhotosAlbum({
      filePath: tempPath,
      success: function (res) {
        wx.showToast({
          title: '保存成功',
          icon: "success"
        })
      },
      fail: function (res) {
        wx.hideLoading()
        wx.getSetting({
          success: function (res) {
            if (!res.authSetting['scope.writePhotosAlbum']) {
              wx.openSetting({})
            }
          }
        })
      }
    })

  }

})